package org.trackcore.handler;

import org.trackcore.model.entity.BehaviorHashRoute;
import org.trackcore.service.BehaviorHashRouteService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class HashRouteChangeHandler implements DataHandler {
    private final BehaviorHashRouteService behaviorHashRouteService;

    public HashRouteChangeHandler(BehaviorHashRouteService behaviorHashRouteService) {
        this.behaviorHashRouteService = behaviorHashRouteService;
    }

    @Override
    public String getHandlerKey() {
        return "behavior_hashchange"; // 对应前端subType
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        BehaviorHashRoute route = BehaviorHashRoute.builder()
                .logId(logId)
                .fromUrl(DataUtils.getString(data, "form")) // 注意前端字段名是form
                .toUrl(DataUtils.getString(data, "to"))
                .starTime(DataUtils.getDouble(data, "StarTime")) // 保持前端大小写
                .uuid(DataUtils.getString(data, "uuid"))
                .build();

        behaviorHashRouteService.save(route);
    }
}